package com.loader;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigLoader {
	
	@SuppressWarnings("unchecked")
	public Map<String, Map<String, String>> loadConfigInfo(String file) {
		Map<String, Map<String,String>> map = new HashMap<String, Map<String,String>>();
		try{
			SAXReader xmlReader = new SAXReader();
			Document doc = xmlReader.read(new File(file));
			
			Element root = doc.getRootElement();
			List<Element> sites = root.elements();
			for(Element site: sites){
				String id = site.attributeValue("id");
				map.put(id,getSiteInfoMap(site));
			}
		}catch(DocumentException e){
			e.printStackTrace();
		}
		
		return map;
	}

	@SuppressWarnings("unchecked")
	private Map<String, String> getSiteInfoMap(Element site){
		Map<String, String> map = new HashMap<String, String>();
		List<Element> list = site.elements();
		for (Element e : list) {
			map.put(e.getName(), e.getText());
		}
		return map;
	}
}
